package com.jinzihan.dao.impl;

import com.jinzihan.dao.OrderDao;
import com.jinzihan.entity.Order;

import java.util.List;

public class OrderDaoImpl extends BaseDao implements OrderDao {
    @Override
    public int saveOrder(Order order) {
        String sql = "insert into t_order(student_id,status,create_time,classroom_id) values(?,?,?,?)";
        return update(sql,order.getStudentId(),order.getStatus(),order.getCreateTime(),order.getClassroomId());
    }

    @Override
    public List<Order> queryMyOrders(int id) {
        String sql = "select id,student_id studentId,status,create_time createTime,classroom_id classroomId from t_order where student_id = ?";
        return queryForList(Order.class,sql,id);
    }

    @Override
    public List<Order> queryAllOrders() {
        String sql = "select id,student_id studentId,status,create_time createTime,classroom_id classroomId from t_order";
        return queryForList(Order.class,sql);
    }

    @Override
    public int changeOrderStatus(int status,int id) {
        String sql = "UPDATE t_order SET STATUS = ? WHERE id = ?";
        return update(sql,status,id);
    }

    @Override
    public int queryClassroomId(int orderId) {
        String sql = "select classroom_id from t_order where id = ?";
        Number classroomId = (Number) queryForSingleValue(sql, orderId);
        return classroomId.intValue();
    }

    @Override
    public int queryForOrderCount() {
        String sql = "select count(*) from t_order";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<Order> queryForPageItems(int begin, int pageSize) {
        String sql = "select id,student_id studentId,status,create_time createTime,classroom_id classroomId from t_order limit ?,?";
        return queryForList(Order.class,sql,begin,pageSize);
    }

    @Override
    public List<Order> queryForMyPageItems(int studentId, int begin, int pageSize) {
        String sql = "select id,student_id studentId,status,create_time createTime,classroom_id classroomId from t_order where student_id = ? limit ?,?";
        return queryForList(Order.class,sql,studentId,begin,pageSize);
    }

    @Override
    public int queryForMyOrderCount(int studentId) {
        String sql = "SELECT COUNT(*) FROM t_order WHERE student_id = ?";
        Number count = (Number) queryForSingleValue(sql, studentId);
        return count.intValue();
    }

    @Override
    public Order queryForOrderById(int id) {
        String sql = "select id,student_id studentId,status,create_time createTime,classroom_id classroomId from t_order where id = ?";
        return queryForOne(Order.class,sql,id);
    }


}
